package com.gobang.domain.vo;

import java.io.Serializable;

/**
 * 分页信息类
 * 用于API响应中的分页数据封装
 * 
 * @author System
 * @since 1.0.0
 */
public class PageInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页码（从1开始）
     */
    private Long current;

    /**
     * 每页显示数量
     */
    private Long size;

    /**
     * 总记录数
     */
    private Long total;

    /**
     * 总页数
     */
    private Long pages;

    /**
     * 是否有上一页
     */
    private Boolean hasPrevious;

    /**
     * 是否有下一页
     */
    private Boolean hasNext;

    /**
     * 无参构造函数
     */
    public PageInfo() {
    }

    /**
     * 构造函数
     *
     * @param current 当前页码
     * @param size 每页显示数量
     * @param total 总记录数
     */
    public PageInfo(Long current, Long size, Long total) {
        this.current = current;
        this.size = size;
        this.total = total;
        this.pages = calculatePages(total, size);
        this.hasPrevious = current > 1;
        this.hasNext = current < this.pages;
    }

    /**
     * 创建分页信息
     *
     * @param current 当前页码
     * @param size 每页显示数量
     * @param total 总记录数
     * @return 分页信息对象
     */
    public static PageInfo of(Long current, Long size, Long total) {
        return new PageInfo(current, size, total);
    }

    /**
     * 创建分页信息
     *
     * @param current 当前页码
     * @param size 每页显示数量
     * @param total 总记录数
     * @return 分页信息对象
     */
    public static PageInfo of(Integer current, Integer size, Long total) {
        return new PageInfo(current.longValue(), size.longValue(), total);
    }

    /**
     * 计算总页数
     *
     * @param total 总记录数
     * @param size 每页显示数量
     * @return 总页数
     */
    private Long calculatePages(Long total, Long size) {
        if (total == null || total == 0 || size == null || size == 0) {
            return 0L;
        }
        return (total + size - 1) / size;
    }

    /**
     * 获取起始记录索引（从0开始）
     *
     * @return 起始记录索引
     */
    public Long getOffset() {
        if (current == null || current <= 0 || size == null || size <= 0) {
            return 0L;
        }
        return (current - 1) * size;
    }

    /**
     * 判断是否为第一页
     *
     * @return 是否为第一页
     */
    public Boolean isFirstPage() {
        return current != null && current.equals(1L);
    }

    /**
     * 判断是否为最后一页
     *
     * @return 是否为最后一页
     */
    public Boolean isLastPage() {
        return current != null && pages != null && current.equals(pages);
    }

    // Getter and Setter methods
    public Long getCurrent() {
        return current;
    }

    public void setCurrent(Long current) {
        this.current = current;
        updateNavigationFlags();
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
        this.pages = calculatePages(this.total, size);
        updateNavigationFlags();
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
        this.pages = calculatePages(total, this.size);
        updateNavigationFlags();
    }

    public Long getPages() {
        return pages;
    }

    public void setPages(Long pages) {
        this.pages = pages;
    }

    public Boolean getHasPrevious() {
        return hasPrevious;
    }

    public void setHasPrevious(Boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    public Boolean getHasNext() {
        return hasNext;
    }

    public void setHasNext(Boolean hasNext) {
        this.hasNext = hasNext;
    }

    /**
     * 更新导航标志
     */
    private void updateNavigationFlags() {
        if (current != null && pages != null) {
            this.hasPrevious = current > 1;
            this.hasNext = current < pages;
        }
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "current=" + current +
                ", size=" + size +
                ", total=" + total +
                ", pages=" + pages +
                ", hasPrevious=" + hasPrevious +
                ", hasNext=" + hasNext +
                '}';
    }
} 